package top.hzy520.reattend.controller;

import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.reattend.entity.ReAttend;
import top.hzy520.reattend.service.ReattendService;
import top.hzy520.user.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 9:13 2018/8/8
 */
@Controller
public class ReattendController {
    @Autowired
    @Qualifier("reattendServiceImpl")
    private ReattendService reattendService;

    @RequestMapping("/reattend/{id}")
    public String reAttend(@PathVariable("id") Long id, Model model){
       model.addAttribute("attendId",id);
       return "admin/attend/ReAttend";
    }
    @ResponseBody
    @RequestMapping("/reattend")
    public ApiResult reAttendFlow(@RequestBody ReAttend reAttend, HttpServletRequest request) throws IOException {
       User user= (User) request.getSession().getAttribute("user");
       reAttend.setReAttendStarter(user.getRealName());
       reattendService.startReattendFlow(reAttend,user);
       ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"补签申请提交成功");
       return apiResult;
    }
    @RequestMapping("/reattend/list")
    public String reAttendList(HttpServletRequest request,Model model){
        User user= (User) request.getSession().getAttribute("user");
       List<ReAttend> reAttendList=reattendService.reAttendList(user.getUsername());
        model.addAttribute("reAttendList",reAttendList);
        return "admin/attend/ReattendList";
    }
    @ResponseBody
    @RequestMapping("/reattend/approve")
    public ApiResult approveReattendFlow(@RequestBody ReAttend reAttend){
          reattendService.approveReattendFlow(reAttend);
           ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"OK");
        return apiResult;
    }
    @RequestMapping("/myreattend/list")
    public String myreattend(Model model){
        User user= (User) SecurityUtils.getSubject().getSession().getAttribute("user");
         List<ReAttend> reAttendList=reattendService.myreattend(user);
         model.addAttribute("reAttendLIst",reAttendList);
        return "admin/attend/myReAttend";
    }
    @RequestMapping("/draw/{id}")
    public void draw(@PathVariable("id") String id,HttpServletResponse response) throws IOException {
      reattendService.draw(id,response);
    }
}
